// subpkg_worker/search/searchName/searchName.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    role:'',
    showList:[],
    elderList:[],
    timer:null,
    keyWord:'',
    //搜索的结果列表
    searchResults:[
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      role:options.role
    })
    this.query()
  },
  //查询对应养老院的所有老人名字
  query(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/elderName/query',
      data:{
        careHomeName:wx.getStorageSync('beadhouse')
      },
      success:(res)=>{
        console.log(res.data.data)
        this.setData({
          elderList:res.data.data
        })
      }
    })
  },
  getkeyWord(e){
    var timer = this.data.timer
  
    clearTimeout(timer)
    
    this.setData({
      timer:timer
    })
    timer=setTimeout(()=>{
      this.setData({
         keyWord:e.detail
      })
      this.getSearchList()
    },500)
    
    this.setData({
      timer:timer
    })
    
  },
  getSearchList(){
    console.log(111)
    var searchResults = []
    if(this.data.keyWord.length===0){
      this.setData({
        searchResults : searchResults,
        showList:[]
      })
      return
    }
    var elderList = this.data.elderList
    var keyWord = this.data.keyWord
    console.log(Object.keys(elderList[0]))
    var showList = elderList.filter(function(item){
      return Object.keys(item)[0].indexOf(keyWord) !== -1
    })
    var showList1 = []
    for(var i in showList){
      var show = {}
      show.name = Object.keys(showList[i])[0]
      show.id = Object.values(showList[i])[0]
      showList1.push(show)
    }
    this.setData({
      showList:showList1
    })
  },
  navigator(e){
    var index = e.currentTarget.dataset.index
    if(this.data.role==-1){
      
      wx.redirectTo({
        url: `/subpkg/my/bindelder/bindelder?id=${this.data.showList[index].id}&name=${this.data.showList[index].name}`,
      })
    }else{
      wx.redirectTo({
        url: `/subpkg_worker/my/bindelder/bindelder?id=${this.data.showList[index].id}&name=${this.data.showList[index].name}`,
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})